package com.aiflow.Action;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.aiflow.db.ConnDB;

public class userConfigdAction {

	// 查询 告警信息
	public void findUserConfig() {
		HttpServletRequest req = ServletActionContext.getRequest();
		int start = Integer.parseInt(req.getParameter("start"));
		int limit = Integer.parseInt(req.getParameter("limit"));
		String name = (String) req.getParameter("userName");
		String para = req.getParameter("para");
		int end = start + limit;
		int begin = start + 1;
		ConnDB db = new ConnDB();
		ResultSet rs = null;
		JSONObject json = new JSONObject();
		List<Map<String, String>> datalist = new ArrayList<Map<String, String>>();
		JSONArray datas = new JSONArray();
		String whereSql = "";
		if(name != null && !name.equals("")){
			whereSql = " and  u.user_name like '%"+name+"%' ";
		}
		String sqlCount = "";
		Connection con = db.getConnection();
		PreparedStatement ps = null;

		try {
			String sql = "select * from "
					+ "("
					+ "select u.id,u.user_name as userName,u.telep ,d.name as targetName,dept.dept_name,dspe.name as spec, "+
					  " (row_number() over(ORDER BY u.id desc)) rb "+
					  " from t_s_userConfigure u,dictionary d ,dictionary dspe, department dept where d.id = u.alarmTarget "+ 
					  " and dspe.id = u.specialty and dept.id = u.region "+whereSql+") tt "
					+ "where tt.rb between " + begin + " and " + end;

			sqlCount = "select count(1) "+
					  " from t_s_userConfigure u,dictionary d ,dictionary dspe, department dept where d.id = u.alarmTarget "+ 
					  " and dspe.id = u.specialty and dept.id = u.region  "+whereSql;
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();

			while (rs.next()) {
				Map<String, String> mapdata = new HashMap<String, String>();
				mapdata.put("id", rs.getString(1));
				mapdata.put("userName", rs.getString(2));
				mapdata.put("telep", rs.getString(3));
				mapdata.put("targetName", rs.getString(4));
				mapdata.put("dept_name", rs.getString(5));
				mapdata.put("spec", rs.getString(6));
				datalist.add(mapdata);
			}
			int index = db.GetInfoCount(sqlCount);
			datas = JSONArray.fromObject(datalist);
			json.put("success", true);
			json.put("datas", datas);
			json.put("results", index);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				ps.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();

			}
		}

	}
 
	// 新增  人员设置
	public void adduserConfig() {
		ConnDB db = new ConnDB();
		PreparedStatement ps = null;
		String user_name = ServletActionContext.getRequest().getParameter(
				"user_name");
		String telep = ServletActionContext.getRequest().getParameter(
				"telep");
		String alarmTarget = ServletActionContext.getRequest().getParameter(
				"alarmTarget");
		String region = ServletActionContext.getRequest().getParameter(
		"region");
		String specialty = ServletActionContext.getRequest().getParameter(
		"specialty");
		String describe = ServletActionContext.getRequest().getParameter(
		"describe");


		Connection con = db.getConnection(); // checkid
		try {
			String sql = "insert into dbo.t_s_userConfigure(user_name,telep,alarmTarget,region,specialty,describe) values(?,?,?,?,?,?)";
			try {
				ps = con.prepareStatement(sql);
				ps.setString(1, user_name);
				ps.setString(2, telep);
				ps.setString(3, alarmTarget);
				ps.setString(4, region);
				ps.setString(5, specialty);
				ps.setString(6, describe);

				int rs = ps.executeUpdate();
				if (rs > 0) {
					// System.out.println("保存成功!!!");
				}
				JSONObject json = new JSONObject();
				json.put("success", true);
				HttpServletResponse response = ServletActionContext
						.getResponse();
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				out.print(json);
				out.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ps.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}
		}
	}

	// 查询 告警对象
	/*
	public void findAlarmsTarget() {
		HttpServletRequest req = ServletActionContext.getRequest();
		int start = Integer.parseInt(req.getParameter("start"));
		int limit = Integer.parseInt(req.getParameter("limit"));
		String name = (String) req.getParameter("name");
		String para = req.getParameter("para");
		int end = start + limit;
		int begin = start + 1;
		ConnDB db = new ConnDB();
		ResultSet rs = null;
		JSONObject json = new JSONObject();
		List<Map<String, String>> datalist = new ArrayList<Map<String, String>>();
		JSONArray datas = new JSONArray();
		String whereSql = "";
		String sqlCount = "";
		Connection con = db.getConnection();
		PreparedStatement ps = null;

		// select t.id,t.alarmTarget_name from t_s_herald h,t_s_alarmTarget t
		// where h.alarm_target = t.id
		// and h.alarm =1
		try {
			String sql = "select * from " + "("
					+ "select t.id,t.alarmTarget_name, "
					+ "(row_number() over(ORDER BY id desc)) rb "
					+ "from t_s_alarmTarget where ) tt "
					+ "where tt.rb between " + begin + " and " + end;
			sqlCount = "select id,alarm_name "
					+ "from t_s_alarmTarget where 1=1 " + whereSql;
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();

			while (rs.next()) {
				Map<String, String> mapdata = new HashMap<String, String>();
				mapdata.put("id", rs.getString(1));
				mapdata.put("alarm_name", rs.getString(2));

				datalist.add(mapdata);
			}
			int index = db.GetInfoCount(sqlCount);
			datas = JSONArray.fromObject(datalist);
			json.put("success", true);
			json.put("datas", datas);
			json.put("results", index);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				ps.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();

			}
		}

	}
	
	*/

	// 修改传报
	public void updateUserConfig() {
		ConnDB db = new ConnDB();
		PreparedStatement ps = null;
		String id = ServletActionContext.getRequest().getParameter(
				"id");
		String user_name = ServletActionContext.getRequest().getParameter(
				"user_name");
		String telep = ServletActionContext.getRequest().getParameter(
				"telep");
		String alarmTarget = ServletActionContext.getRequest().getParameter(
				"alarmTarget");
		String region = ServletActionContext.getRequest().getParameter(
				"region");
		String specialty = ServletActionContext.getRequest().getParameter(
				"specialty");
		String describe = ServletActionContext.getRequest().getParameter("describe");
		String sql = "update t_s_userConfigure set user_name = ?,telep= ? ,alarmTarget =?,region= ?,specialty=?,describe=? where id = ?";
		Connection con = db.getConnection();
			try {
				ps = con.prepareStatement(sql);
				int index = 0;
				ps.setString(1, user_name);
				ps.setString(2, telep);
				ps.setString(3, alarmTarget);
				ps.setString(4, region);
				ps.setString(5, specialty);
				ps.setString(6, describe);
				ps.setString(7, id);
				int rs = ps.executeUpdate();
				if (rs > 0) {
					// System.out.println("保存成功!!!");
				}
				JSONObject json = new JSONObject();
				json.put("success", true);
				HttpServletResponse response = ServletActionContext
						.getResponse();
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				out.print(json);
				out.close();
		 
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					try {
						ps.close();
						con.close();
					} catch (Exception ex1) {
						ex1.printStackTrace();
					}
				}
	}

	// 删除 传报
	public void deleteUserConfig() {
		ConnDB db = new ConnDB();
		Statement ps = null;
		HttpServletRequest req = ServletActionContext.getRequest();
		String id = req.getParameter("userConfIds");

		String sqld = "";
		id = id.substring(0, id.length() - 1);
		Connection con = db.getConnection();
		try {
			con.setAutoCommit(false);
			ps = con.createStatement();

			boolean bool = false;

			String sql = "delete t_s_userConfigure  where id in( " + id + " )";
			bool = ps.execute(sql);
			ps = con.createStatement();
			if (bool) {
				//System.out.println("保存成功!!!");
			}
			JSONObject json = new JSONObject();
			json.put("success", true);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		} finally {
			try {
				con.commit();
				ps.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}
		}
	}

	// 获取选中传报的对象
	public void getUserConfig() {
		ConnDB db = new ConnDB();
		ResultSet rs = null;
		List<Map<String, String>> datalist = new ArrayList<Map<String, String>>();
		JSONArray datas = new JSONArray();
		String UserConfigId = ServletActionContext.getRequest()
				.getParameter("userConfId");
		Connection con = db.getConnection();
		PreparedStatement ps = null;
		try {
			String sql = "select  id,user_name,telep,alarmTarget,region,specialty,describe from t_s_userConfigure u where u.id = "+ UserConfigId;

			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			Map<String, String> mapdata = new HashMap<String, String>();
			try {
				while (rs.next()) {

					mapdata.put("id", rs.getString(1));
					mapdata.put("user_name", rs.getString(2));
					mapdata.put("telep", rs.getString(3));
					mapdata.put("alarmTarget", rs.getString(4));
					mapdata.put("region", rs.getString(5));
					mapdata.put("specialty", rs.getString(6));
					mapdata.put("describe", rs.getString(7));
				 
				}
				JSONObject json = JSONObject.fromObject(mapdata);
				datas = JSONArray.fromObject(datalist);
				json.put("success", true);
				json.put("datas", datas);
				HttpServletResponse response = ServletActionContext
						.getResponse();
				response.setCharacterEncoding("UTF-8");
				PrintWriter out = response.getWriter();
				out.print(json.toString());
				out.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ps.close();
				rs.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}
		}
	}

	// 或取上次保存的值
	public List getHeralSelected(String heraldId) {
		ConnDB db = new ConnDB();
		ResultSet rs = null;
		List list = new ArrayList<Map<String, String>>();
		List<Map<String, String>> datalist = new ArrayList<Map<String, String>>();
		JSONArray datas = new JSONArray();

		Connection con = db.getConnection();
		PreparedStatement ps = null;
		try {
			String sql = "select h.id,d.name,alarm,alarm_target,target_name   from dbo.t_s_herald h,dictionary d where h.alarm = d.id and h.id = "
					+ heraldId;

			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			Map<String, String> mapdata = new HashMap<String, String>();
			String target = "";
			while (rs.next()) {

				target = rs.getString(4);

				list.add(target);

			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				ps.close();
				rs.close();
				con.close();
			} catch (Exception ex1) {
				ex1.printStackTrace();
			}
			return list;
		}
	}

}
